﻿
Partial Class WebForm_ApproveVolunteer
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.BindData()
        End If
    End Sub

    Protected Sub SearchNewVolunteer_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchNewVolunteer1.SelectedIndexChanged
        '單筆檢視模式
        If (Me.SearchNewVolunteer1.SelecteDataSource = "R") Then
            Me.TabContainer1.ActiveTab = Me.AddTabPanel
            Me.AddVolunteer1.InitData(Me.SearchNewVolunteer1.SelectedVolunteerID)
        ElseIf (Me.SearchNewVolunteer1.SelecteDataSource = "E") Then
            Me.TabContainer1.ActiveTab = Me.ApproveEnlistRecordTabPanel
            Me.AddVolunteer2.GetVOrganizationID = Me.SearchNewVolunteer1.VOrganizationID
            Me.AddVolunteer2.EnlistHeaderID = Me.SearchNewVolunteer1.SelectedEnlistID
            Me.AddVolunteer2.InitData(Me.SearchNewVolunteer1.SelectedGV2VolunteerID)
        End If
    End Sub

    Protected Sub SearchNewVolunteer_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchNewVolunteer1.DataBound
        If (Me.SearchNewVolunteer1.SelecteDataSource = "R") Then
            If Me.SearchNewVolunteer1.Count > 0 Then
                If Me.SearchNewVolunteer1.SelectedStatus = My.FormStatus.Approved Then
                    Me.AddVolunteer1.DataAccessMode = My.VolunteerDataAccessMode.Approved
                ElseIf Me.SearchNewVolunteer1.SelectedStatus = My.FormStatus.Rejected Then
                    Me.AddVolunteer1.DataAccessMode = My.VolunteerDataAccessMode.Rejected
                Else
                    Me.AddVolunteer1.DataAccessMode = My.VolunteerDataAccessMode.Approve
                End If

                Return

                Me.AddVolunteer1.InitData(Me.SearchNewVolunteer1.SelectedVolunteerID)
            End If
        ElseIf (Me.SearchNewVolunteer1.SelecteDataSource = "E") Then
            If Me.SearchNewVolunteer1.Count2 > 0 Then

                If Me.SearchNewVolunteer1.SelectedStatus = My.FormStatus.Approved Then
                    Me.AddVolunteer2.DataAccessMode = My.VolunteerDataAccessMode.Approved
                ElseIf Me.SearchNewVolunteer1.SelectedStatus = My.FormStatus.Rejected Then
                    Me.AddVolunteer2.DataAccessMode = My.VolunteerDataAccessMode.Rejected
                Else
                    Me.AddVolunteer2.DataAccessMode = My.VolunteerDataAccessMode.Approve
                End If

                Return

                Me.AddVolunteer2.InitData(Me.SearchNewVolunteer1.SelectedGV2VolunteerID)
            End If
        End If

    End Sub

#Region "觸發事件"

#Region "新註冊事件"
    Protected Sub AddVolunteer1_Approving(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddVolunteer1.Approving
        Me.Approve()
    End Sub
    Protected Sub AddVolunteer1_Rejecting(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddVolunteer1.Rejecting
        Me.Reject()
    End Sub
    Protected Sub AddVolunteer1_Saving(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddVolunteer1.Saving
        Me.Save()
    End Sub
#End Region

#Region "招募活動事件"
    Protected Sub AddVolunteer2_Approving(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddVolunteer2.Approving
        Me.Approve()
    End Sub
    Protected Sub AddVolunteer2_Rejecting(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddVolunteer2.Rejecting
        Me.Reject()
    End Sub
    Protected Sub AddVolunteer2_Saving(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddVolunteer2.Saving
        Me.Save()
    End Sub
#End Region

    Protected Sub Approve()
        If Me.ApproveData("Y") = True Then
            '列表檢視模式
            Me.TabContainer1.ActiveTabIndex = 0
            Me.SearchNewVolunteer1.BindGV()
            MyObj.ShowMessage(Me.GetLocalResourceObject("ApproveData_Success"))
        End If
    End Sub
    Protected Sub Reject()
        If Me.ApproveData("N") = True Then
            '列表檢視模式
            Me.TabContainer1.ActiveTabIndex = 0
            Me.SearchNewVolunteer1.BindGV()
            MyObj.ShowMessage(Me.GetLocalResourceObject("RejectData_Success"))
        End If
    End Sub
    Protected Sub Save()
        If Me.SaveData = True Then
            '列表檢視模式
            Me.TabContainer1.ActiveTabIndex = 0
            Me.SearchNewVolunteer1.BindGV()
            MyObj.ShowMessage(Me.GetLocalResourceObject("SaveData_Success"))
        End If
    End Sub

#End Region
End Class

Partial Class WebForm_ApproveVolunteer
    Private Sub BindData()

    End Sub

    Private Function SaveData() As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope
            '新註冊審核
            If (Me.SearchNewVolunteer1.SelecteDataSource = "R") Then

                sql.Length = 0

                '希望所屬志工隊
                sql.AppendFormat(" Delete from WishOrganization Where VolunteerID='{0}'; ", Me.AddVolunteer1.VolunteerID)
                Dim CheckCount As Integer = 0
                For i As Integer = 0 To Me.AddVolunteer1.WishOrganization.Length - 1
                    If (Me.AddVolunteer1.WishOrganization(i) <> "") Then
                        sql.AppendFormat("insert WishOrganization (VolunteerID,OrganizationID) select ")
                        sql.AppendFormat(" '{0}' ", Me.AddVolunteer1.VolunteerID)
                        sql.AppendFormat(",'{0}'; ", Me.AddVolunteer1.WishOrganization(i).ToString())
                        CheckCount += 1
                    End If
                Next
                If CheckCount > MyObj.GetParameter("Register", "WishOrganizationCount") Then
                    MyObj.ShowMessage(Me.GetLocalResourceObject("OverWishOrganization"))
                    Return False
                End If


                sql.AppendFormat("update Volunteer set ")
                sql.AppendFormat(" VolunteerID='{0}' ", Me.AddVolunteer1.VolunteerID)
                sql.AppendFormat(",EcolifeID='{0}' ", Me.AddVolunteer1.EcolifeID)
                sql.AppendFormat(",VolunteerName='{0}' ", Me.AddVolunteer1.VolunteerName)
                'sql.AppendFormat(",Password='{0}' ", clsAuth.EncryptPassword(Me.AddVolunteer1.VolunteerPassword))
                sql.AppendFormat(",IdentificationID='{0}' ", Me.AddVolunteer1.VolunteerID)
                sql.AppendFormat(",IdentificationType='{0}' ", Me.AddVolunteer1.IdentificationType)
                sql.AppendFormat(",UpdateDate={0} ", "getdate()")
                sql.AppendFormat(",UpdateBy='{0}' ", Me.LoginID)
                sql.AppendFormat(",Gender='{0}' ", Me.AddVolunteer1.Gender)
                sql.AppendFormat(",Birthday='{0:yyyy/MM/dd}' ", Me.AddVolunteer1.Birthday)
                sql.AppendFormat(",MobilePhone='{0}' ", Me.AddVolunteer1.MobilePhone)
                sql.AppendFormat(",OfficePhone='{0}' ", Me.AddVolunteer1.OfficePhone)
                sql.AppendFormat(",BirthVillageID='{0}' ", Me.AddVolunteer1.BirthRegionID)
                sql.AppendFormat(",BirthAddress='{0}' ", Me.AddVolunteer1.BirthAddress)
                sql.AppendFormat(",SameAddress='{0}' ", Me.AddVolunteer1.SameAddress)
                sql.AppendFormat(",VillageID='{0}' ", Me.AddVolunteer1.RegionID)
                sql.AppendFormat(",Address='{0}' ", Me.AddVolunteer1.Address)
                sql.AppendFormat(",EmergentPerson='{0}' ", Me.AddVolunteer1.EmergentPerson)
                sql.AppendFormat(",EmergentPhone='{0}' ", Me.AddVolunteer1.EmergentPhone)
                sql.AppendFormat(",Email='{0}' ", Me.AddVolunteer1.Email)
                sql.AppendFormat(",EducationLevel='{0}' ", Me.AddVolunteer1.EducationLevel)
                sql.AppendFormat(",OccupationType='{0}' ", Me.AddVolunteer1.OccupationType)
                sql.AppendFormat(",Retired='{0}' ", Me.AddVolunteer1.Retired)
                sql.AppendFormat("where VolunteerID='{0}'; ", Me.AddVolunteer1.OriginalVolunteerID)


                sql.AppendFormat("update VolunteerApplication set ")
                sql.AppendFormat(" VolunteerID='{0}' ", Me.AddVolunteer1.VolunteerID)
                sql.AppendFormat(",UpdateDate={0} ", "getdate()")
                sql.AppendFormat(",UpdateBy='{0}' ", Me.LoginID)
                sql.AppendFormat("where VolunteerID='{0}'; ", Me.AddVolunteer1.OriginalVolunteerID)


                '基礎訓練時數
                sql.AppendFormat(" update OtherTrainingRecord set ")
                sql.AppendFormat(" TrainingHour = '{0}' ", Me.AddVolunteer1.TrainingHour(0))
                sql.AppendFormat(" Where VolunteerID='{0}' ", Me.AddVolunteer1.VolunteerID)
                sql.AppendFormat(" And TrainingType='B'; ")

                '特殊訓練時數
                sql.AppendFormat(" update OtherTrainingRecord set ")
                sql.AppendFormat(" TrainingHour = '{0}' ", Me.AddVolunteer1.TrainingHour(1))
                sql.AppendFormat(" Where VolunteerID='{0}' ", Me.AddVolunteer1.VolunteerID)
                sql.AppendFormat(" And TrainingType='S'; ")

                '在職訓練時數
                sql.AppendFormat(" update OtherTrainingRecord set ")
                sql.AppendFormat(" TrainingHour = '{0}' ", Me.AddVolunteer1.TrainingHour(2))
                sql.AppendFormat(" Where VolunteerID='{0}' ", Me.AddVolunteer1.VolunteerID)
                sql.AppendFormat(" And TrainingType='W'; ")

                '服務時數累計
                sql.AppendFormat(" update OtherWorkRecord set ")
                sql.AppendFormat(" WorkHour = '{0}' ", Me.AddVolunteer1.OtherWorkHour)
                sql.AppendFormat(" Where VolunteerID='{0}' ", Me.AddVolunteer1.VolunteerID)

                dat.AccData(sql)

                '招募審核
            Else
                'sql.AppendFormat("update EnlistRecordApplication set ")
                'sql.AppendFormat(" VOrganizationID='{0}' ", Me.AddVolunteer2.VOrganizationID)
                'sql.AppendFormat(" where VolunteerID='{0}' ", Me.AddVolunteer2.OriginalVolunteerID)
                'sql.AppendFormat(" and EnlistID='{0}' ", Me.SearchNewVolunteer1.SelectedEnlistID)
                'sql.AppendFormat(" and Status='{0}' ", "WA")
                'dat.AccData(sql)
            End If

            scope.Complete()
        End Using

        Return True
    End Function

    Private Function ApproveData(ByVal approveResult As String) As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Dim volunteerStatus As My.VolunteerStatus
        Dim formStatus As My.FormStatus

        If approveResult = "Y" Then
            volunteerStatus = My.VolunteerStatus.Approved
            formStatus = My.FormStatus.Approved
        Else
            volunteerStatus = My.VolunteerStatus.Rejected
            formStatus = My.FormStatus.Rejected
        End If

        Using scope As New Transactions.TransactionScope
            sql.Length = 0
            If (Me.SearchNewVolunteer1.SelecteDataSource = "R") Then '新註冊
                Dim basicTrainingHour As Integer = MyObj.GetParameter("VBook", "BasicTrainingHour")
                Dim specialTrainingHour As Integer = MyObj.GetParameter("VBook", "SpecialTrainingHour")
                Dim workTrainingHour As Integer = MyObj.GetParameter("VBook", "WorkTrainingHour")

                If Me.AddVolunteer1.TrainingHour(0) >= basicTrainingHour _
                    And Me.AddVolunteer1.TrainingHour(1) >= specialTrainingHour _
                    And Me.AddVolunteer1.TrainingHour(2) >= workTrainingHour _
                    And approveResult = "Y" Then
                    volunteerStatus = My.VolunteerStatus.VBooked
                End If

                sql.AppendFormat("update Volunteer set ")
                sql.AppendFormat(" Active='{0}' ", volunteerStatus)

                If approveResult = "Y" Then
                    'sql.AppendFormat(",VOrganizationID='{0}' ", Me.AddVolunteer1.VOrganization)

                    '此處請改為 Insert VOrganizationMapping = WishOrganization下之所有志工隊
                End If

                sql.AppendFormat(",UpdateDate={0} ", "getdate()")
                sql.AppendFormat(",UpdateBy='{0}' ", Me.LoginID)
                sql.AppendFormat("where VolunteerID='{0}'; ", Me.AddVolunteer1.OriginalVolunteerID)
                dat.AccData(sql)

                sql.Length = 0
                sql.AppendFormat("update VolunteerApplication set ")
                sql.AppendFormat(" Status='{0}' ", formStatus)
                sql.AppendFormat(",UpdateDate={0} ", "getdate()")
                sql.AppendFormat(",UpdateBy='{0}' ", Me.LoginID)
                sql.AppendFormat("where VolunteerID='{0}'; ", Me.AddVolunteer1.OriginalVolunteerID)
                dat.AccData(sql)


                If approveResult = "Y" Then

                    sql.Length = 0

                    '希望所屬志工隊
                    sql.AppendFormat(" Delete from VOrganizationMapping Where VolunteerID='{0}'; ", Me.AddVolunteer1.VolunteerID)
                    Dim CheckCount As Integer = 0
                    For i As Integer = 0 To Me.AddVolunteer1.WishOrganization.Length - 1
                        If (Me.AddVolunteer1.WishOrganization(i) <> "") Then
                            sql.AppendFormat("insert VOrganizationMapping (VolunteerID,VOrganizationID,EffectDate) select ")
                            sql.AppendFormat(" '{0}' ", Me.AddVolunteer1.VolunteerID)
                            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WishOrganization(i).ToString())
                            sql.AppendFormat(",'{0}'; ", "9999/12/31")
                            CheckCount += 1
                        End If
                    Next
                    If CheckCount > MyObj.GetParameter("Register", "WishOrganizationCount") Then
                        MyObj.ShowMessage(Me.GetLocalResourceObject("OverWishOrganization"))
                        Return False
                    End If

                    '基礎訓練時數
                    sql.AppendFormat(" update OtherTrainingRecord set ")
                    sql.AppendFormat(" Active='Y' ")
                    sql.AppendFormat(",UpdateDate={0} ", "getdate()")
                    sql.AppendFormat(",UpdateBy='{0}' ", Me.LoginID)
                    sql.AppendFormat(",TrainingHour = '{0}' ", Me.AddVolunteer1.TrainingHour(0))
                    sql.AppendFormat(" Where VolunteerID='{0}' ", Me.AddVolunteer1.OriginalVolunteerID)
                    sql.AppendFormat(" And TrainingType='B'; ")

                    '特殊訓練時數
                    sql.AppendFormat(" update OtherTrainingRecord set ")
                    sql.AppendFormat(" Active='Y' ")
                    sql.AppendFormat(",UpdateDate={0} ", "getdate()")
                    sql.AppendFormat(",UpdateBy='{0}' ", Me.LoginID)
                    sql.AppendFormat(",TrainingHour = '{0}' ", Me.AddVolunteer1.TrainingHour(1))
                    sql.AppendFormat(" Where VolunteerID='{0}' ", Me.AddVolunteer1.OriginalVolunteerID)
                    sql.AppendFormat(" And TrainingType='S'; ")

                    '在職訓練時數
                    sql.AppendFormat(" update OtherTrainingRecord set ")
                    sql.AppendFormat(" Active='Y' ")
                    sql.AppendFormat(",UpdateDate={0} ", "getdate()")
                    sql.AppendFormat(",UpdateBy='{0}' ", Me.LoginID)
                    sql.AppendFormat(",TrainingHour = '{0}' ", Me.AddVolunteer1.TrainingHour(2))
                    sql.AppendFormat(" Where VolunteerID='{0}' ", Me.AddVolunteer1.OriginalVolunteerID)
                    sql.AppendFormat(" And TrainingType='W'; ")

                    '已服務時數
                    sql.AppendFormat("update OtherWorkRecord set ")
                    sql.AppendFormat(" Active='Y' ")
                    sql.AppendFormat(",UpdateDate={0} ", "getdate()")
                    sql.AppendFormat(",UpdateBy='{0}' ", Me.LoginID)
                    sql.AppendFormat(",WorkHour = '{0}' ", Me.AddVolunteer1.OtherWorkHour)
                    sql.AppendFormat("where VolunteerID='{0}'; ", Me.AddVolunteer1.OriginalVolunteerID)

                    dat.AccData(sql)


                    ''新增至服務時數 (Table來源應該是OtherWorkRecord，已不可考)
                    'sql.AppendFormat("insert WorkHourRecord (RecordID,WorkType,VolunteerID,WorkHour,WorkTimes,Active,EffectiveDate.CreateDate.CreateBy) select ")
                    'sql.AppendFormat(" RecordID,'Register',VolunteerID,WorkHour,1,'Y',getdate(),getdate() ")
                    'sql.AppendFormat(",CreateBy='{0}' ", Me.LoginID)
                    'sql.AppendFormat("where VolunteerID='{0}' ", Me.AddVolunteer1.OriginalVolunteerID)
                    'dat.AccData(sql)

                End If

                clsFlow.AddApprover("VolunteerApplication", Me.SearchNewVolunteer1.SelectedApplicationID, Me.LoginID)
                clsFlow.Approve(MyObj.GetIdentity(), approveResult)
            ElseIf (Me.SearchNewVolunteer1.SelecteDataSource = "E") Then '招募活動
                sql.AppendFormat("update EnlistRecordApplication set ")
                If approveResult = "Y" Then
                    sql.AppendFormat(" Status='{0}' ", "AP")
                Else
                    sql.AppendFormat(" Status='{0}' ", "RJ")
                End If
                'sql.AppendFormat(" ,VOrganizationID='{0}' ", Me.AddVolunteer2.VOrganizationID)
                sql.AppendFormat(" where VolunteerID='{0}' ", Me.AddVolunteer2.OriginalVolunteerID)
                sql.AppendFormat(" and EnlistID='{0}' ", Me.SearchNewVolunteer1.SelectedEnlistID)
                sql.AppendFormat(" and Status='{0}' ", "WA")
                dat.AccData(sql)

                If approveResult = "Y" Then
                    sql.Length = 0
                    sql.AppendFormat("insert into EnlistRecord(EnlistID,VolunteerID,Active,CreateDate,CreateBy) values(")
                    sql.AppendFormat("'{0}'", Me.SearchNewVolunteer1.SelectedEnlistID)
                    sql.AppendFormat(",'{0}'", Me.AddVolunteer2.OriginalVolunteerID)
                    sql.AppendFormat(",'Y'")
                    sql.AppendFormat(",{0}", "getdate()")
                    sql.AppendFormat(",'{0}'", Me.LoginID)
                    sql.AppendFormat(")")
                    dat.AccData(sql)

                    'sql.Length = 0
                    'sql.AppendFormat("Insert into VOrganizationMapping select ")
                    'sql.AppendFormat(" '{0}'", Me.AddVolunteer2.OriginalVolunteerID)
                    'sql.AppendFormat(",'{0}'", Me.AddVolunteer2.VOrganizationID)
                    'sql.AppendFormat(",'{0:yyyy/MM/dd}'", Me.AddVolunteer2.EndDate)
                    'sql.AppendFormat(" where not exists(")
                    'sql.AppendFormat(" select * from VOrganizationMapping")
                    'sql.AppendFormat(" where VolunteerID='{0}'", Me.AddVolunteer2.OriginalVolunteerID)
                    'sql.AppendFormat(" and VOrganizationID='{0}'", Me.AddVolunteer2.VOrganizationID)
                    'sql.AppendFormat(" )")
                    'dat.AccData(sql)

                    'sql.Length = 0
                    'sql.AppendFormat("Update VOrganizationMapping set")
                    'sql.AppendFormat(" EffectDate='{0:yyyy/MM/dd}'", Me.AddVolunteer2.EndDate)
                    'sql.AppendFormat(" where VolunteerID='{0}'", Me.AddVolunteer2.OriginalVolunteerID)
                    'sql.AppendFormat(" and VOrganizationID='{0}'", Me.AddVolunteer2.VOrganizationID)
                    'sql.AppendFormat(" and EffectDate<'{0:yyyy/MM/dd}'", Me.AddVolunteer2.EndDate)
                    'dat.AccData(sql)
                End If

                clsFlow.AddApprover("EnlistRecordApplication", Me.SearchNewVolunteer1.SelectedApplicationID2, Me.LoginID)
                clsFlow.Approve(MyObj.GetIdentity(), approveResult)

            End If

            scope.Complete()
        End Using

        Return True
    End Function

    Private ReadOnly Property LoginID() As String
        Get
            Return Context.User.Identity.Name
        End Get
    End Property

End Class